package pers.w.chaptertalk.funs;

import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

import pers.w.chaptertalk.funs.db.Tab_Chapter;

public class ChapterInfo {
    private static final String TAG = "ChapterInfo";

    public long getChapterID() {
        return Long.parseLong(chapterID);
    }

    public String getName() {
        return name;
    }

    public String getUri() {
        return uri;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;// new ArrayList<>();
        //this.content.addAll(Arrays.asList(content.split(" ")));

    }

    public File chapterFile;
    private String chapterID, name, uri;
    private String content = null;

    public static final String TAG_STR = "\u3000\u3000\n\u3000";

    public ChapterInfo(File bookFile, Map<String, String> chapter) {

        assert chapter != null;
        chapterID = chapter.get(Tab_Chapter.CHAPTER_ID);
        if (null==chapterID && Fun.isDebug()) {
            for (Map.Entry<String,String> c :chapter.entrySet()) {
                Log.d(TAG, c.getKey()+" "+c.getValue());

            }
        }
        name = chapter.get(Tab_Chapter.CHAPTERNAME);
        uri = chapter.get(Tab_Chapter.CHAPTERURI);


        chapterFile = new File(bookFile , chapterID);
        getContentFromFile();

    }

    private void getContentFromFile() {
        if (chapterFile.exists()) {
            try {
                FileInputStream fis = new FileInputStream(chapterFile);
                InputStreamReader is = new InputStreamReader(fis);
                BufferedReader reader = new BufferedReader(is);
                String line;
                //content = new ArrayList<>();
                //分行读取
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    sb.append(line).append("\n");
                }
                content = sb.toString();

                reader.close();
                is.close();
                fis.close();


            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
    public void delFile() {
        if (chapterFile.exists()) chapterFile.delete();
        content=null;
    }

    public void save() {
        try {

            BufferedWriter writer = new BufferedWriter(new FileWriter(chapterFile, false));
            //for (int i = 0; i < content.size(); i++) {
            writer.write(content);
            // writer.newLine();
            //}
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
